/**
 * Created by sunny.w <iwisunny@gmail.com> on 15-7-28.
 */
(function(){
    var city_wrap=$('#city_list'),
        district_wrap=$('#district_list'),
        zone_wrap=$('#zone_list');

    wxi.map([city_wrap, district_wrap, zone_wrap], function(item){
       wxi.shopBundle.fillSelect(item,[]);
    });

    //获取城市列表，填充城市下拉框
    var city_data={};
    wxi.shopBundle.getCityList(function(){
        city_data=wxi.util.getCache('cityData');
        wxi.shopBundle.fillSelect(city_wrap,city_data);

        //改变城市，动态修改对应的地区
        city_wrap.on('change',function(){
            var cur_city=$(this).val();
            var district=city_data[cur_city] || [];

            wxi.shopBundle.fillSelect(district_wrap, district);
            district_wrap.val(0).trigger('change');
        });

        district_wrap.on('change',function(){
            var cur_city=city_wrap.val(),
                cur_dist=$(this).val();

            var zone= city_data[cur_city] && city_data[cur_city][cur_dist] ?
                city_data[cur_city][cur_dist] : [];

            wxi.shopBundle.fillSelect(zone_wrap, zone);
            zone_wrap.val(0).trigger('change');
        });

        if(city_name && district_name ){

            city_wrap.val(city_name).trigger('change');
            district_wrap.val(district_name).trigger('change');
            zone_wrap.val(zone_name).trigger('change');
        }
    });

    //加载editor
    wxi.shopBundle.registerEditor($('#ueditor'), 'ueditor', function(ue){
        var cont=$('#shop_intro').html();
        ue.setContent(wxi.string.htmlEncode(cont));
    });

    //添加门店，表单判断
    $('#btn_submit_form').on('click',function(){
        var fm=$(this).parents('form');
        var intro=$('#shop_intro');
        var picture=$('#shop_picture');

        var entity=wxi.shopBundle.
            preCheckForm(fm, 'shop_adminbundle_shop');

        entity.intro=UE.getEditor('ueditor').getContent();

        console.log(entity);

        if(!entity.name){
            wxi.util.tipMsg('', '请填写门店名');
            return false;
        }
        if(entity.city == 0){
            wxi.util.tipMsg('', '请选择城市');
            return false;
        }
        if(entity.district == 0){
            wxi.util.tipMsg('', '请选择地区');
            return false;
        }
        if(entity.shopZone == 0){
            wxi.util.tipMsg('', '请选择商圈');
            return false;
        }
        if(entity.brandId == 0){
            wxi.util.tipMsg('', '请选择品牌');
            return false;
        }
        if(entity.address == ''){
            wxi.util.tipMsg('', '请填写详细地址');
            return false;
        }
        if(entity.contact == ''){
            wxi.util.tipMsg('', '请填写商家电话');
            return false;
        }
        if(entity.workTime == ''){
            wxi.util.tipMsg('', '请填写营业时间');
            return false;
        }
        if(entity.latitude == ''){
            wxi.util.tipMsg('', '请填写经度');
            return false;
        }
        if(entity.longitude == ''){
            wxi.util.tipMsg('', '请填写纬度');
            return false;
        }

        if(!picture.prev().hasClass('img-container')){
            //new brand page
            if(!picture.val()){
                wxi.util.tipMsg('', '请上传门店图片');
                return false;
            }
        }

        if(!entity.intro){
            wxi.util.tipMsg('', '请填写门店简介');
            return false;
        }
        else{
            fm.find('#shop_intro').html(entity.intro);
        }

        //start submitting
        fm.submit();

    });


})();